<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh">
<head>
  <meta charset="utf-8">
  <title>磁力搜索</title>
  <link rel="stylesheet" href="layui/css/layui.css" media="all">
  <style type="text/css">
	.table-button {
		height: 24px;
		line-height: 24px;
		width: 60px;
		margin-right: 20px;
	}
	body{
		margin-left:auto;
		margin-right:auto;
		margin-top:2%;
		text-align:center;
		width:90%;
		max-width:1200px;
	}
	.big-logo{
		display:none;
	}
  </style>
</head>
<body>
<div class="search-box">
	<img alt="磁力搜索" class="big-logo" src="static/img/logo.png"/>
	<form class="layui-form" action="" style="padding-left:10px;padding-right:10px;">
	  <div class="layui-inline" style="width:79%;">
		<input id="search-keyword" type="text" name="keyword" lay-verify="title" autocomplete="off" placeholder="请输入标题" class="layui-input">
	  </div>
	  <div class="layui-inline" style="width:20%;">
		<button id="search-btn" style="width:100%" class="layui-btn" lay-submit="" lay-filter=""><i class="layui-icon">&#xe615;</i>搜索</button>
	  </div>  
	</form>
</div>
<table id="demo" lay-filter="test"></table>
<script src="layui/layui.js"></script>
<script src="static/js/clipboard.min.js"></script>
<script src="static/js/jquery.min.js"></script>
<script src="static/js/functions.js"></script>
<script type="text/html" id="toolBarTpl">
  <div class="layui-btn-container">
	<button data-clipboard-text="magnet:?xt=urn:btih:{{d.info_hash}}" type="button" class="btn-copy table-button layui-btn layui-btn-sm layui-btn-radius">
		<span>复制</span>
	</button>
	<button onclick="showDetails('{{d.info_hash}}','{{d.name}}','此文件发布时间为：<b>{{d.create_time}}</b>，大小为 <b>{{d.length}}</b>');" type="button" class="table-button layui-btn layui-btn-sm layui-btn-radius">
		<span>详情</span>
	</button>
	<a href="" target="_blank">
		<button type="button" class="table-button layui-btn layui-btn-sm layui-btn-radius"><span>播放</span></button>
	</a>
  </div>
</script>
<script>
function showDetails(hash, bttitle, info){
	layui.use('layer', function(){
		let layer = layui.layer;
		const iframe_address = 'info.html?hash=' + hash +'&bttitle=' + encodeURI(bttitle) + '&info=' + encodeURI(info);
		//iframe层
		layer.open({
			type: 2,
			title: bttitle + ' - 详细信息',
			shadeClose: true,
			shade: 0.8,
			area: ['80%', '90%'],
			content: iframe_address //iframe的url
		}); 	  
	});	
}
let clipboard = new ClipboardJS('.btn-copy');

clipboard.on('success', function(e) {
	layui.use('layer', function(){ //独立版的layer无需执行这一句
		layer.msg('复制成功');
	});
	console.log(e);
});

clipboard.on('error', function(e) {
	console.log(e);
});
$(document).ready(function() {	
	let api_address = 'api.php?keyword=';
	const keyword = getParameterByName('keyword');
	if(keyword == null || keyword == ''){
		$($('.big-logo')[0]).show();
	}else{
		$($('.big-logo')[0]).hide();
		$("#search-keyword").val(keyword);
		api_address += encodeURI(keyword);
		const cat = getParameterByName('category');
		let catIsSet = false;
		if(cat == null || cat === ''){
			catIsSet = false;
		}else {
			catIsSet = true;
			api_address += "&category=" + cat;
		}
		layui.use('table', function(){
		  let table = layui.table;
		  
		  table.render({
			elem: '#demo'
			,limit: 20
			,url: api_address //数据接口
			,loading: true
			,method: 'get'
			,page: true //开启分页
			,text: {
				none: '唉啊，没有找到呢。换一个关键词试一下吧！' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
			}
			,request: {
				pageName: 'page' //页码的参数名称，默认：page
				,limitName: 'count' //每页数据量的参数名，默认：limit
			}
			,cols: [[ //表头
			  {type: 'numbers', title: '#', width:80, fixed: 'left'}
			  ,{title: '名称', minWidth:400, sort: true, align: 'left', templet: '<div><a href="magnet:?xt=urn:btih:{{d.info_hash}}" class="layui-table-link">{{d.name}}</a></div>'}
			  ,{field: 'length', title: '大小', width:100, sort: true}
			  ,{field: 'create_time', title: '发布时间', width: 110, sort: true} 
			  ,{title: '类型', width: 75, sort: true, templet: function(d){
				let cat_name = catEn2Zh(d.cat_value);
				return '<a href="' + (catIsSet ?(window.location.href):(window.location.href + "&category=" + d.cat_value)) + '" class="layui-table-link">' + cat_name + '</a>';
			  }}
			  ,{title: '操作', width: 280, toolbar: '#toolBarTpl'}
			]]
			,parseData: function(res){ //res 即为原始返回的数据
				let data = res.result.items;
				for(let i = 0; i < data.length; i++)
				{
					data[i].create_time = timestamp2local(data[i].create_time * 1000);
					data[i].length = fileLength2Size(1 * data[i].length);
				}
				return {
				  "code": 0, //解析接口状态
				  "msg": 'message', //解析提示文本
				  "count": res.result.meta.total, //解析数据长度
				  "data": data //解析数据列表
				};
			}
		  });  
		});

	}
});
</script>
</body>
</html>